﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using RoadTeam.Model;
using RoadTeam.Enums;
using RoadTeam.SysInterface;
using RoadTeam.Pub;

namespace RoadTeam.BLL
{

    public class SysUserBLL : BllBase
    {
        private static object o = new object();
        /// <summary>
        /// 获取登录人信息
        /// </summary>
        /// <param name="LoginID">登录账号</param>
        /// <param name="Pwd">登录密码</param>
        /// <returns>登录人数据集合</returns>
        public DataTable GetLoginInfo(string LoginID, String Pwd)
        {
            base.InserLog("登陆账号:" + LoginID + "密码:" + Pwd);
            SqlWhere where = new SqlWhere();
            SysUser user = new SysUser();
            user.UserLoginID = LoginID;
            user.UserPwd = Pwd;
            where.Info = user;
            DataTable dt = SysUser.Select(where, GlobalConfig.ConnectionString);
            //插入登录记录
            return dt;
        }

        public int ReflashStaffData()
        {
            DataTable dt_User = GetEmptyTable();
            DataRow dr_User;
            lock (o)
            {
                DataTable dt_Staff = FactoryClass.CreateSmStaff().GetStaffTable();
                foreach (DataRow dr in dt_Staff.Rows)
                {
                    dr[5] = CreateIdentifier.GetIdentifier();
                    dr_User = dt_User.NewRow();
                    dr_User["UserIdentifier"] = dr[5];
                    dr_User["UserName"] = dr[1];
                    dr_User["UserLoginID"] = dr[4];
                    dr_User["UserPwd"] = 111111;
                    dr_User["UserTypeName"] = dr[6];
                    dr_User["OrganIdentifier"] = FactoryClass.CreateSmOrgan().GetIdentitiferByID(dr[3].ToString());
                    dt_User.Rows.Add(dr_User);
                }
                FactoryClass.CreateSmStaff().UpdateStaffTable(dt_Staff);
            }
            SysUser.InsertDatatable(dt_User, GlobalConfig.ConnectionString);
            return dt_User.Rows.Count;
        }

        public DataTable GetEmptyTable()
        {
            SqlWhere where = new SqlWhere();
            where .Info= new SysUser(){UserIdentifier=@" -1 "};
            where.SelectColumn = @" UserIdentifier,UserName ,UserLoginID,UserPwd,UserTypeName,OrganIdentifier  ";
           return  SysUser.Select(where,GlobalConfig.ConnectionString);
        }




    }
}
